﻿Imports System.Globalization
Public Class Trade
    Dim sBuyFlags() As String = {"b", "buy", "s", "sell"}

#Region "Properties"

    Private _Broker As Broker
    Public Property Broker() As Broker
        Get
            Return _Broker
        End Get
        Set(ByVal value As Broker)
            _Broker = value
        End Set
    End Property

    Private _Fund As Fund
    Public Property Fund() As Fund
        Get
            Return _Fund
        End Get
        Set(ByVal value As Fund)
            _Fund = value
        End Set
    End Property

    Private _SessionType As String
    Public Property SessionType() As String
        Get
            Return _SessionType
        End Get
        Set(ByVal value As String)
            _SessionType = value
        End Set
    End Property

    Private _OrderType As String
    Public Property OrderType() As String
        Get
            Return _OrderType
        End Get
        Set(ByVal value As String)
            _OrderType = value
        End Set
    End Property


    Private _DateTime As String
    Public Property DateTime() As String
        Get
            Return _DateTime
        End Get
        Set(ByVal value As String)
            _DateTime = value
        End Set
    End Property

    Private _CLSBypassFlag As String
    Public Property CLSBypassFlag() As String
        Get
            Return _CLSBypassFlag
        End Get
        Set(ByVal value As String)
            _CLSBypassFlag = value
        End Set
    End Property

    Private _Side As String
    Public Property Side() As String
        Get
            Return _Side
        End Get
        Set(ByVal value As String)
            _Side = value
        End Set
    End Property

    Private _TradeDate As Date
    Public Property TradeDate() As Date
        Get
            Return _TradeDate
        End Get
        Set(ByVal value As Date)
            _TradeDate = value
        End Set
    End Property

    Private _ValueDate As Date
    Public Property ValueDate() As Date
        Get
            Return _ValueDate
        End Get
        Set(ByVal value As Date)
            _ValueDate = value
        End Set
    End Property


    Private _QuotePair As String
    Public Property QuotePair() As String
        Get
            Return _QuotePair
        End Get
        Set(ByVal value As String)
            _QuotePair = value
        End Set
    End Property


    Private _GivenCCY As String
    Public Property GivenCCY() As String
        Get
            Return _GivenCCY
        End Get
        Set(ByVal value As String)
            _GivenCCY = value
        End Set
    End Property


    Private _GivenAmt As Double
    Public Property GivenAmt() As Double
        Get
            Return _GivenAmt
        End Get
        Set(ByVal value As Double)
            _GivenAmt = value
        End Set
    End Property

    Private _CounterCCY As String
    Public Property CounterCCY() As String
        Get
            Return _CounterCCY
        End Get
        Set(ByVal value As String)
            _CounterCCY = value
        End Set
    End Property

    Private _CounterAmt As Double
    Public Property CounterAmt() As Double
        Get
            Return _CounterAmt
        End Get
        Set(ByVal value As Double)
            _CounterAmt = value
        End Set
    End Property

    Private _ClientID As String
    Public Property ClientID() As String
        Get
            Return _ClientID
        End Get
        Set(ByVal value As String)
            _ClientID = value
        End Set
    End Property


    Private _Comments As String
    Public Property Comments() As String
        Get
            Return _Comments
        End Get
        Set(ByVal value As String)
            _Comments = value
        End Set
    End Property

    Private _SptFwdFlag As String
    Public Property SptFwdFlag() As String
        Get
            Return _SptFwdFlag
        End Get
        Set(ByVal value As String)
            _SptFwdFlag = value
        End Set
    End Property

    Private _OCFlag As String
    Public Property OCFlag() As String
        Get
            Return _OCFlag
        End Get
        Set(ByVal value As String)
            _OCFlag = value
        End Set
    End Property

    Private _SpotRate As Double
    Public Property SpotRate() As Double
        Get
            Return _SpotRate
        End Get
        Set(ByVal value As Double)
            _SpotRate = value
        End Set
    End Property

    Private _FwdPts As Double
    Public Property FwdPts() As Double
        Get
            Return _FwdPts
        End Get
        Set(ByVal value As Double)
            _FwdPts = value
        End Set
    End Property

    Private _AllInRate As Double
    Public Property AllInRate() As Double
        Get
            Return _AllInRate
        End Get
        Set(ByVal value As Double)
            _AllInRate = value
        End Set
    End Property

    Private _SwapPts As Double
    Public Property SwapPts() As Double
        Get
            Return _SwapPts
        End Get
        Set(ByVal value As Double)
            _SwapPts = value
        End Set
    End Property

    Private _FixingDate As Date
    Public Property FixingDate() As Date
        Get
            Return _FixingDate
        End Get
        Set(ByVal value As Date)
            _FixingDate = value
        End Set
    End Property

    Private _FXCOrderID As String
    Public Property FXCOrderID() As String
        Get
            Return _FXCOrderID
        End Get
        Set(ByVal value As String)
            _FXCOrderID = value
        End Set
    End Property

    Private _BlockID As String
    Public Property BlockID() As String
        Get
            Return _BlockID
        End Get
        Set(ByVal value As String)
            _BlockID = value
        End Set
    End Property

    Private _ClientBlockID As String
    Public Property ClientBlockID() As String
        Get
            Return _ClientBlockID
        End Get
        Set(ByVal value As String)
            _ClientBlockID = value
        End Set
    End Property

    Private _Status As String
    Public Property Status() As String
        Get
            Return _Status
        End Get
        Set(ByVal value As String)
            _Status = value
        End Set
    End Property

    Private sData1 As String
    Public Property Data1() As String
        Get
            Return sData1
        End Get
        Set(ByVal value As String)
            sData1 = value
        End Set
    End Property
    Private sData2 As String
    Public Property Data2() As String
        Get
            Return sData2
        End Get
        Set(ByVal value As String)
            sData2 = value
        End Set
    End Property
    Private sData3 As String
    Public Property Data3() As String
        Get
            Return sData3
        End Get
        Set(ByVal value As String)
            sData3 = value
        End Set
    End Property
    Private sData4 As String
    Public Property Data4() As String
        Get
            Return sData4
        End Get
        Set(ByVal value As String)
            sData4 = value
        End Set
    End Property
    Private sData5 As String
    Public Property Data5() As String
        Get
            Return sData5
        End Get
        Set(ByVal value As String)
            sData5 = value
        End Set
    End Property

    Private sCustom1 As String
    Public Property Custom1() As String
        Get
            Return sCustom1
        End Get
        Set(ByVal value As String)
            sCustom1 = value
        End Set
    End Property
    Private sCustom2 As String
    Public Property Custom2() As String
        Get
            Return sCustom2
        End Get
        Set(ByVal value As String)
            sCustom2 = value
        End Set
    End Property
    Private sCustom3 As String
    Public Property Custom3() As String
        Get
            Return sCustom3
        End Get
        Set(ByVal value As String)
            sCustom3 = value
        End Set
    End Property
    Private sCustom4 As String
    Public Property Custom4() As String
        Get
            Return sCustom4
        End Get
        Set(ByVal value As String)
            sCustom4 = value
        End Set
    End Property
    Private sCustom5 As String
    Public Property Custom5() As String
        Get
            Return sCustom5
        End Get
        Set(ByVal value As String)
            sCustom5 = value
        End Set
    End Property

    Private sCustodianFromFXC As String
    Public Property CustodianFromFXC() As String
        Get
            Return sCustodianFromFXC
        End Get
        Set(ByVal value As String)
            sCustodianFromFXC = value
        End Set
    End Property

    Private sLink As String
    Public Property Link() As String
        Get
            Return sLink
        End Get
        Set(ByVal value As String)
            sLink = value
        End Set
    End Property

    Private sLimitType As String
    Public Property LimitType() As String
        Get
            Return sLimitType
        End Get
        Set(ByVal value As String)
            sLimitType = value
        End Set
    End Property

    Private sReqRate As String
    Public Property ReqRate() As String
        Get
            Return sReqRate
        End Get
        Set(ByVal value As String)
            sReqRate = value
        End Set
    End Property

    Private sExpType As String
    Public Property ExpType() As String
        Get
            Return sExpType
        End Get
        Set(ByVal value As String)
            sExpType = value
        End Set
    End Property

    Private sZone As String
    Public Property Zone() As String
        Get
            Return sZone
        End Get
        Set(ByVal value As String)
            sZone = value
        End Set
    End Property

    Private sExpDate As String
    Public Property ExpDate() As String
        Get
            Return sExpDate
        End Get
        Set(ByVal value As String)
            sExpDate = value
        End Set
    End Property

    Private sExpTime As String
    Public Property ExpTime() As String
        Get
            Return sExpTime
        End Get
        Set(ByVal value As String)
            sExpTime = value
        End Set
    End Property

    Private sFillAll As String
    Public Property FillAll() As String
        Get
            Return sFillAll
        End Get
        Set(ByVal value As String)
            sFillAll = value
        End Set
    End Property
    Private _AmtFilled As String
    Public Property AmtFilled() As String
        Get
            Return _AmtFilled
        End Get
        Set(ByVal value As String)
            _AmtFilled = value
        End Set
    End Property

    Private _EffRate As String
    Public Property EffRate() As String
        Get
            Return _EffRate
        End Get
        Set(ByVal value As String)
            _EffRate = value
        End Set
    End Property

    Private _Action As String
    Public Property Action() As String
        Get
            Return _Action
        End Get
        Set(ByVal value As String)
            _Action = value
        End Set
    End Property

    Private _Operation As String
    Public Property Operation() As String
        Get
            Return _Operation
        End Get
        Set(ByVal value As String)
            _Operation = value
        End Set
    End Property

    Private _NetFlag As String
    Public Property NetFlag() As String
        Get
            Return _NetFlag
        End Get
        Set(ByVal value As String)
            _NetFlag = value
        End Set
    End Property

    Private _CommoneRef As String
    Public Property CommonRef() As String
        Get
            Return _CommoneRef
        End Get
        Set(ByVal value As String)
            _CommoneRef = value
        End Set
    End Property

    Private _SSIs As SSI
    Public Property SSIs() As SSI
        Get
            Return _SSIs
        End Get
        Set(ByVal value As SSI)
            _SSIs = value
        End Set
    End Property


    Private _SenderToReceiver As String
    Public Property SenderToReceiver() As String
        Get
            Return _SenderToReceiver
        End Get
        Set(ByVal value As String)
            _SenderToReceiver = value
        End Set
    End Property


    Private _Upload As Boolean
    Public Property Upload() As Boolean
        Get
            Return _Upload
        End Get
        Set(ByVal value As Boolean)
            _Upload = value
        End Set
    End Property



#End Region

#Region "Private Methods"


#End Region

#Region "Public Methods"

    Public Sub New()
        'Me.Upload = True
    End Sub

    Sub GenerateTradeID()
        If My.Settings.sCurrentDate = String.Empty OrElse _
        Not My.Settings.sCurrentDate = String.Format("{0:yyyyMMdd}", Today()) Then
            My.Settings.sCurrentDate = String.Format("{0:yyyyMMdd}", Today())
            My.Settings.sCurrentCount = 0
        Else
            My.Settings.sCurrentCount += 1
        End If
        Me.ClientID = My.Settings.sCurrentDate & My.Settings.sCurrentCount
    End Sub
    Sub SwitchToBuySell()
        Select Case Me.Side.ToLower.Trim
            Case "b", "buy"
            Case "s", "sell"
            Case Else
                Exit Sub
        End Select
    End Sub
    Function GetSideDetails(ByVal BuyOrSell As String) As SideDetail

        Dim sd As New SideDetail

        If Me.Side = BuyOrSell.ToUpper.Trim Then
            sd.CCY = Me.GivenCCY
            sd.Amt = Me.GivenAmt
        Else
            sd.CCY = Me.CounterCCY
            sd.Amt = Me.CounterAmt
        End If

        Return sd

    End Function

    Sub NormalizeSide()
        Select Case Me.Side.ToLower.Trim
            Case "b", "buy", "by"
                Me.Side = "BUY"
            Case "s", "sell", "sl"
                Me.Side = "SELL"
            Case Else
                Exit Sub
        End Select
    End Sub

    Sub NormalizeSptFwd()
        If Not Strings.InStr(Me.SptFwdFlag.ToUpper, "FWD", CompareMethod.Text) = 0 Then
            Me.SptFwdFlag = "Fwd"
        ElseIf Not Strings.InStr(Me.SptFwdFlag.ToUpper, "SPOT", CompareMethod.Text) = 0 Then
            Me.SptFwdFlag = "Spot"
        ElseIf Not Strings.InStr(Me.SptFwdFlag.ToUpper, "FWRD", CompareMethod.Text) = 0 Then
            Me.SptFwdFlag = "Fwd"
        ElseIf Not Strings.InStr(Me.SptFwdFlag.ToUpper, "SPT", CompareMethod.Text) = 0 Then
            Me.SptFwdFlag = "Spot"
        Else
            If Not Strings.InStr(Me.SptFwdFlag.ToUpper, "F", CompareMethod.Text) = 0 Then
                Me.SptFwdFlag = ("Fwd")
            Else
                Me.SptFwdFlag = "Spot"
            End If
        End If
    End Sub

    Sub TrimTradeID()
        If Me.ClientID.Length > 16 AndAlso (Not Me.ClientID = String.Empty OrElse Me.ClientID IsNot Nothing) Then
            Me.ClientID.Trim()
            Me.ClientID.Replace("_", "")
        End If
    End Sub

    Public Shared Function ConvertEuroDate(ByVal EuroDate As String) As Date

        Dim sDate As String
        sDate = Split(EuroDate, "/")(1) & "/" & Split(EuroDate, "/")(0) & "/" & Right(EuroDate, 4)

        sDate = String.Format("{0:dd/MM/yyyy}", sDate)
        Return sDate
    End Function

    Public Shared Function GetInstitution(ByVal FXCTriad As String) As String
        Dim asTriad() As String = FXCTriad.Split(".")
        Return asTriad(1)
    End Function

    Public Shared Function GetUser(ByVal FXCTriad As String) As String
        Dim asTriad() As String = FXCTriad.Split(".")
        Return asTriad(2)
    End Function

    Public Shared Function ConvertDate(ByVal myDate As String, ByVal DateFormat As String) As Date
        Dim myCulture As New CultureInfo("en-US")
        Try
            Return Date.ParseExact(myDate, DateFormat, myCulture)
        Catch exFrmt As FormatException
            frmLoader.LoaderError.LogError(exFrmt, "Date Conversion")
        Catch ex As Exception
            frmLoader.LoaderError.LogError(ex, "Date Conversion")
        End Try
    End Function
#Region "Spot Fwd"

    Sub sGLCIDModifier(ByVal aTrade As Trade)
        Dim sGLCIDModifier As String

        sGLCIDModifier = "/GLCID/" & aTrade.Fund.FundName
        If My.Settings.bUploadSpotFwd = True AndAlso aTrade.SptFwdFlag <> "" Then
            sGLCIDModifier = sGLCIDModifier & "|/" & aTrade.SptFwdFlag.ToUpper & "/"
        End If

        If My.Settings.bUploadSpotFwd = True AndAlso aTrade.Side <> "" Then
            If Left(aTrade.Side.ToUpper, 1) = "B" Then
                aTrade.Side = "BUY"
            Else
                aTrade.Side = "SELL"
            End If
            If InStr(sGLCIDModifier, "/FWD") > 0 Or InStr(sGLCIDModifier, "/SPOT") > 0 Then
                sGLCIDModifier = sGLCIDModifier & "" & aTrade.Side & "/"
            Else
                sGLCIDModifier = sGLCIDModifier & "|/" & aTrade.Side & "/"
            End If
        End If
        If My.Settings.sBIC.ToLower.Trim = "lbamchixxxx" Then
            sGLCIDModifier = sGLCIDModifier & "|" & aTrade.Comments
        End If
        aTrade.SenderToReceiver = sGLCIDModifier
        'function created as a space to add future options to the GLCID field
    End Sub



    Sub GetSpotFwd()
        Dim sSpot As String = "Spot"
        Dim sFwd As String = "Fwd"
        Dim sSpots() As String = {"spot", "spt"}
        Dim sFwds() As String = {"fwrd", "fwd", "forward"}
        If Left(Me.SptFwdFlag, 1).ToLower = "s" Then Me.SptFwdFlag = sSpot
        If Left(Me.SptFwdFlag, 1).ToLower = "F" Then Me.SptFwdFlag = sFwd

        For Each s As String In sSpots
            If Me.SptFwdFlag.ToLower.Contains(s) Then
                Me.SptFwdFlag = sSpot
            End If
        Next

        For Each f As String In sFwds
            If Me.SptFwdFlag.ToLower.Contains(f) Then
                Me.SptFwdFlag = sFwd
            End If
        Next

        Select Case My.Settings.sSpotFwdOption.ToLower
            Case "all spot"
                Me.SptFwdFlag = sSpot
            Case "all forward"
                Me.SptFwdFlag = sFwd

            Case "specify spot days"
                Dim tsSpot As TimeSpan = Me.ValueDate.Subtract(Me.TradeDate)
                If tsSpot.Days > My.Settings.iSpotDays Then
                    Me.SptFwdFlag = sFwd
                Else
                    Me.SptFwdFlag = sSpot
                End If
            Case "user ccys and holidays"
                CalendarSpotCalc()
        End Select
    End Sub

    Private Sub CalendarSpotCalc()
        Dim iGivenSpotDays, iCounterSpotDays, iSpotDays As Integer
        Dim bPassedWeekend As Boolean = False

        For Each drCCYDays As dsCCYSpotDays.CCYSpotDaysRow In My.Settings.CCYSpotDays(0)
            If drCCYDays.CCY = Me.GivenCCY Then iGivenSpotDays = drCCYDays.SpotDays
            If drCCYDays.CCY = Me.CounterCCY Then iCounterSpotDays = drCCYDays.SpotDays
        Next

        IIf(iGivenSpotDays > iCounterSpotDays, iSpotDays = iGivenSpotDays, iSpotDays = iCounterSpotDays)

        If iSpotDays = Nothing OrElse iSpotDays = 0 Then

            ' (Me.GivenCCY & " or " & Me.CounterCCY & " is not on the currency spot days list.  A default of 2 days will be used.  Please add ASAP.")
            iSpotDays = 2
        End If

        Dim dSpotDate As Date = Me.TradeDate.AddDays(iSpotDays)

        For Each drHoliday As dsHolidays.HolidaysRow In My.Settings.Holidays(0)
            Dim dHoliday As Date = drHoliday.HolidayDate
            Dim sCCYs As String() = drHoliday.CCYs.Split(",")
            If (sCCYs.Contains(Me.GivenCCY) OrElse sCCYs.Contains(Me.CounterCCY)) AndAlso _
            Date.Compare(Me.TradeDate, dHoliday) < 0 AndAlso _
            Date.Compare(dHoliday, dSpotDate) <= 0 Then
                dSpotDate = dSpotDate.AddDays(1)
            End If

            If Date.Compare(dHoliday, Me.ValueDate) > 0 Then Exit For
        Next

        Select Case Weekday(dSpotDate, FirstDayOfWeek.Sunday)
            Case 1 ' sunday
                If iSpotDays = 2 Then
                    dSpotDate = dSpotDate.AddDays(2)
                Else
                    dSpotDate = dSpotDate.AddDays(1)
                End If
                bPassedWeekend = True
            Case 7 ' saturday
                dSpotDate = dSpotDate.AddDays(2)
                bPassedWeekend = True
            Case 2 'monday
                If iSpotDays = 3 Then
                    dSpotDate = dSpotDate.AddDays(2)
                    bPassedWeekend = True
                End If

        End Select

        If bPassedWeekend = True Then
            Dim dSundayBefore As Date = dSpotDate ' finds the sunday of the next week as a basis for 2nd Holiday check
            Do Until Weekday(dSundayBefore, FirstDayOfWeek.Sunday) = 1
                dSundayBefore = dSundayBefore.AddDays(-1)
            Loop
            For Each drHoliday As dsHolidays.HolidaysRow In My.Settings.Holidays(0)
                Dim dHoliday As Date = drHoliday.HolidayDate
                Dim sCCYs As String() = drHoliday.CCYs.Split(",")
                If (sCCYs.Contains(Me.GivenCCY) OrElse sCCYs.Contains(Me.CounterCCY)) AndAlso _
                Date.Compare(dSpotDate, dHoliday) >= 0 AndAlso _
                Date.Compare(dSundayBefore, dHoliday) < 0 Then
                    dSpotDate = dSpotDate.AddDays(1)
                End If

                If Date.Compare(dHoliday, Me.ValueDate) > 0 Then Exit For
            Next

        End If
        If dSpotDate < Me.ValueDate Then
            Me.SptFwdFlag = "Fwd"
        Else
            Me.SptFwdFlag = "Spot"
        End If
    End Sub
#End Region

#End Region



End Class

Public Class SideDetail

    Private _CCY As String
    Public Property CCY() As String
        Get
            Return _CCY
        End Get
        Set(ByVal value As String)
            _CCY = value
        End Set
    End Property

    Private _Amt As Double
    Public Property Amt() As Double
        Get
            Return _Amt
        End Get
        Set(ByVal value As Double)
            _Amt = value
        End Set
    End Property


End Class
